package leo.weixin.controller;

import leo.weixin.service.IWxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
public class WxController {
     //
//    @RequestMapping("/wx")
//    public void Wx(HttpServletRequest req){
//        System.out.println(req.getMethod());
//        System.out.println("微信请求...");
//    }

   @Autowired
   private IWxService iWxService;

    /**
     * 验证消息的确来自微信服务器
     * 参数	描述
     * signature	微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
     * timestamp	时间戳
     * nonce	随机数
     * echostr	随机字符串
     */
    @GetMapping("/wx")
    public String Wx(@RequestParam("signature") String signature,@RequestParam("timestamp") String timestamp,
                     @RequestParam("nonce")String nonce,@RequestParam("echostr") String echostr){
        return iWxService.check(signature, timestamp, nonce, echostr);
    }

    /**
     * 处理用户消息请求
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping("/wx")
    public String Wx(HttpServletRequest request) throws IOException {
        return iWxService.deal(request.getInputStream());
    }

}
